package com.nbcio.im.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nbcio.common.core.utils.StringUtils;
import com.nbcio.im.core.service.ImUserService;
import com.nbcio.system.domain.SysUser;
import com.nbcio.system.domain.vo.SysUserVo;
import com.nbcio.system.domain.bo.SysUserBo;
import com.nbcio.system.mapper.SysUserMapper;
import com.nbcio.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * IM用户服务实现
 * 直接使用系统用户，只管理IM相关字段
 *
 * @author nbacheng
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ImUserServiceImpl implements ImUserService {
    
    private final ISysUserService sysUserService;
    private final SysUserMapper sysUserMapper;
    
    @Override
    public SysUserVo getSysUser(Long userId) {
        // 获取系统用户信息
        return sysUserService.selectUserById(userId);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserStatus(Long userId, String status) {
        // 直接更新sys_user表的IM相关字段
        SysUserBo sysUserBo = new SysUserBo();
        sysUserBo.setUserId(userId);
        sysUserBo.setImStatus(status);
        if ("online".equals(status)) {
            sysUserBo.setImLastOnlineTime(LocalDateTime.now());
        }
        sysUserService.updateUser(sysUserBo);
        
        log.info("更新用户IM状态: {} -> {}", userId, status);
    }
    
    @Override
    public List<SysUserVo> getOnlineUsers() {
        // 查询在线用户 - 直接使用sys_user表
        SysUserBo searchUser = new SysUserBo();
        searchUser.setImStatus("online");
        searchUser.setStatus("0"); // 系统用户启用状态
        
        List<SysUserVo> sysUsers = sysUserService.selectUserList(searchUser);
        return sysUsers.stream()
                .filter(user -> "online".equals(user.getImStatus()))
                .collect(Collectors.toList());
    }
    
    @Override
    public List<SysUserVo> searchUsers(String keyword) {
        if (StringUtils.isEmpty(keyword)) {
            return List.of();
        }
        
        // 搜索系统用户
        SysUserBo searchUser = new SysUserBo();
        searchUser.setUserName(keyword);
        searchUser.setNickName(keyword);
        searchUser.setEmail(keyword);
        searchUser.setPhonenumber(keyword);
        
        List<SysUserVo> sysUsers = sysUserService.selectUserList(searchUser);
        return sysUsers.stream()
                .filter(user -> "0".equals(user.getStatus())) // 只返回启用的用户
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLastOnlineTime(Long userId) {
        // 更新最后在线时间
        SysUserBo sysUserBo = new SysUserBo();
        sysUserBo.setUserId(userId);
        sysUserBo.setImLastOnlineTime(LocalDateTime.now());
        sysUserService.updateUser(sysUserBo);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateImUserInfo(Long userId, String imSignature, String imGender, LocalDateTime imBirthday, String imRegion) {
        // 更新IM用户信息
        SysUserBo sysUserBo = new SysUserBo();
        sysUserBo.setUserId(userId);
        sysUserBo.setImSignature(imSignature);
        sysUserBo.setImGender(imGender);
        sysUserBo.setImBirthday(imBirthday);
        sysUserBo.setImRegion(imRegion);
        sysUserService.updateUser(sysUserBo);
    }
    
    @Override
    public String getUserImStatus(Long userId) {
        SysUserVo sysUserVo = sysUserService.selectUserById(userId);
        return sysUserVo != null ? sysUserVo.getImStatus() : "offline";
    }
}
